//
//  PlayerAndNetSetViewController.swift
//  MyTest
//
//  Created by guanzhongwang on 2021/5/7.
//

import UIKit

class PlayerAndNetSetViewController:CommonViewController {
    
    private let tableView = UITableView.init(frame: CGRect.zero, style: .grouped)
    private var dataList:Array<SetItemModel> = []
    private var tiplist:Array<Array<String>> = []
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        self.title = "播放与网络设置"
        self.view.addSubview(tableView)
        tableView.snp.makeConstraints({
            $0.edges.equalToSuperview()
        })
        tableView.separatorColor = Separator_COLOR
        tableView.backgroundColor = BACKGROUND_COLOR

        tableView.delegate = self
        tableView.dataSource = self
        tableView.register(SetItemArrowCell.self, forCellReuseIdentifier: "SettingArrowCell")
        tableView.estimatedSectionHeaderHeight = 0
        tableView.estimatedSectionFooterHeight = 0
        tableView.rowHeight = 44.0
        
        dataList = [SetItemModel.init(type: .Arrow, title: "非WiFi网络流量",tip: "最佳效果"),
                         SetItemModel.init(type: .Arrow, title: "非WiFi网络播放提醒",tip: "提醒一次"),
                         SetItemModel.init(type: .Arrow, title: "频道自动播放下一个视频",tip: "开启")]
        tiplist.append(["最佳效果(下载大图)","较省流量(智能下图)","及省流量(不下载图)"])
        tiplist.append(["每次提醒","提醒一次"])
        tiplist.append(["开启","关闭"])

    }

}
extension PlayerAndNetSetViewController:UITableViewDelegate{
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 10.0
    }
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0.001
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 44.0
    }
   
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        SystemLog("didSelectRowAt")
        tableView.deselectRow(at: indexPath, animated: false)
        let item = dataList[indexPath.row]
        let sheet = UIAlertController.init(title: item.title, message: nil, preferredStyle: .actionSheet)
        for name in tiplist[indexPath.row]{
            let al = UIAlertAction.init(title: name, style: .default) { [weak self] (action) in
                self?.dataList = (self?.dataList.map({ (temp) -> SetItemModel in
                    if temp.title == item.title{
                        return SetItemModel.init(type: temp.type, title: temp.title,tip: action.title!)
                    }
                    return temp
                }))!
                self?.tableView.reloadData()
            }
            sheet.addAction(al)
        }
        sheet.addAction(UIAlertAction.init(title: "取消", style: .cancel))
        self.present(sheet, animated: true) {
            
        }
    }
    
}
extension PlayerAndNetSetViewController:UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataList.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let item = dataList[indexPath.row]
        let cell = tableView.dequeueReusableCell(withIdentifier: "SettingArrowCell", for: indexPath) as? SetItemArrowCell
        cell?.binding(item)
        return cell!
    }
    
    @objc func linkclick() {
        SystemLog("linkclick")
    }
}


